import datetime
import zipfile
import os
import windnd
import tkinter as tk


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('压缩文件')
        self.wm_attributes('-topmost', 1)
        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()

        ww = 600
        wh = 580
        self.geometry(
            "%dx%d+%d+%d" %
            (ww, wh, (sw - ww) / 2, (sh - wh) / 2))
        self.after(1, lambda: self.focus_force())
        tk.Label(self, text='请将待压缩文件拖入下方窗口', font=('微软雅黑', 20)).pack()
        e_frame = tk.Frame(self)
        e_frame.pack()
        self.croll = tk.Scrollbar(e_frame, orient=tk.VERTICAL)
        self.croll.pack(fill='y', side=tk.RIGHT)
        self.e = tk.Text(e_frame, width=50, font=('微软雅黑', 10), yscrollcommand=self.croll.set)

        # windows 挂钩
        windnd.hook_dropfiles(self.e.winfo_id(), lambda l: [self.e.insert("end", i.decode("gbk") + '\n') for i in l])
        self.e.pack(side=tk.RIGHT)
        self.croll.config(command=self.e.yview)
        b_frame = tk.Frame(self)
        b_frame.pack()
        self.btn = tk.Button(b_frame, text='压缩', width=15, height=2,
                             activebackground='grey', relief='groove')
        self.btn.bind('<ButtonRelease-1>', self.ok)

        self.btn.pack(side=tk.LEFT)
        self.btn = tk.Button(b_frame, text='清空', width=15, height=2,
                             activebackground='grey', relief='groove')
        self.btn.bind('<ButtonRelease-1>', self.clear)

        self.btn.pack(side=tk.LEFT)
        self.btn_cancel = tk.Button(b_frame, text='退出', width=15, height=2,
                                    activebackground='grey', relief='groove')
        self.btn_cancel.bind('<ButtonRelease-1>', self.cancel)

        self.btn_cancel.pack(side=tk.LEFT)

    def ok(self, event):
        monday, sunday = datetime.date.today(), datetime.date.today()
        one_day = datetime.timedelta(days=1)
        while monday.weekday() != 0:
            monday -= one_day
        while sunday.weekday() != 5:
            sunday += one_day
        # 返回当前的星期一和星期天的日期
        monday = monday.strftime("%m.%d")
        sunday = sunday.strftime("%m.%d")
        files = self.e.get(1.0, "end")
        files_list = files.strip('\n').split('\n')
        num = len(files_list)
        if not os.path.exists('.\\周报压缩包'):
            os.mkdir('.\\周报压缩包')
        file = f'.\\周报压缩包\\豫南{num}人周报汇总{monday}-{sunday}.zip'
        # 遍历files文件夹下的文件，压缩发送
        zip_1 = zipfile.ZipFile(file, 'w')
        for f in files_list:
            zip_1.write(f, os.path.basename(f), zipfile.ZIP_DEFLATED)
        zip_1.close()
        os.startfile('.\\周报压缩包')
        self.destroy()

    def cancel(self, event):
        self.destroy()

    def clear(self, event):
        self.e.delete(1.0, "end")


if __name__ == '__main__':
    # monday, sunday = get_current_week()
    # zip_them(monday, sunday)

    a = MainWindow()
    a.mainloop()
